package com.dwxt.cdms.util.wx;

import com.dwxt.cdms.exception.AesException;
import com.dwxt.cdms.util.XMLUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;

public class CheckUtil {


    static final String token="dwxt2019";
    static final String encodingAesKey="dsBtr3ttt63rH3OpKOCx3MCB32XSr2C6oMqxQcmZmsx";
    static final String appId="wxfcc4d2ccf2c52016";

    public static boolean checkSignature(String signature,String timestamp,String nonce) {
        //根据微信的规则验证 是否接入成功
        String[] arr=new String[] {token, timestamp,nonce};
        Arrays.sort(arr);
        StringBuffer content= new StringBuffer();
        for(int i=0;i<arr.length;i++) {
            content.append(arr[i]);
        }
        String temp=getSha1(content.toString());//加密
        return temp.equals(signature);
    }


       public static String getSha1(String str) {  
            if (str == null || str.length() == 0) {  
                return null;  
            }  
            char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
                    'a', 'b', 'c', 'd', 'e', 'f' };  

            try {  
                MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
                mdTemp.update(str.getBytes("UTF-8"));  

                byte[] md = mdTemp.digest();  
                int j = md.length;  
                char buf[] = new char[j * 2];  
                int k = 0;  
                for (int i = 0; i < j; i++) {  
                    byte byte0 = md[i];  
                    buf[k++] = hexDigits[byte0 >>> 4 & 0xf];  
                    buf[k++] = hexDigits[byte0 & 0xf];  
                }  
                return new String(buf);  
            } catch (Exception e) {  
                return null;  
            }  
        }


    /**
     * 加密回复消息
     */

    public static String encryptMsg(String msg,String timestamp,String nonce) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        String mingwen = pc.encryptMsg(msg, timestamp, nonce);
        System.out.println("加密后: " + mingwen);
        return mingwen;
    }
    /**
     * 解密请求消息
     */
    public static String decryptMsg(String msg,String timestamp,String nonce,String signature) throws AesException, ParserConfigurationException, IOException, SAXException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        Document document = XMLUtil.readString2XML(msg);
        Element root = document.getRootElement();
        String encrypt = XMLUtil.readNode(root, "Encrypt");
        String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
        String fromXML = String.format(format, encrypt);
        // 第三方收到公众号平台发送的消息
        String result2 = pc.decryptMsg(signature, timestamp, nonce, fromXML);
        System.out.println("解密后明文: " + result2);
        return result2;
    }

}
